package com.mrzhou.study.poi;

import io.jsonwebtoken.lang.Assert;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

public class ForecastExcelImport {

    private XSSFWorkbook workbook;

    public ForecastExcelImport(String uri) {
        try{
//            URL url = new URL(uri);
//            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//            connection.connect();
//            InputStream is = connection.getInputStream();
            workbook = new XSSFWorkbook(new FileInputStream(uri)); // 创建workBook
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** 获取excel中sheet中对应的数据 */
    public void getFirstSheetData() {
        XSSFSheet sheet =  workbook.getSheetAt(0);
        Assert.notNull(sheet, "excel中至少包含一个工作簿！");
        Assert.state(sheet.getLastRowNum() > 0, "excel至少包含标题" );
        Map<String, Integer> map = headerIndexMap(sheet);
        for(int i =0; i < sheet.getLastRowNum(); i++) {

        }


    }

    public Map<String, Integer> headerIndexMap(XSSFSheet sheet) {
        Map<String, Integer> map = new HashMap<>();
        Row row = sheet.getRow(0); // 获取第一行的数据
        int i = 0;
        Cell value = row.getCell(i);
        while(!StringUtils.equalsIgnoreCase("DataMeasure", value.getStringCellValue())) {
            map.put(StringUtils.lowerCase(value.getStringCellValue()), i);
            i++;
            row.getCell(i);
        }
        return map;
    }

    public static void main(String[] args) {
        ForecastExcelImport excelImport = new ForecastExcelImport("https://osstest.minmaxtec.com/66615405/upload/20210201/bc967bf53daf471553cb237e40447e2d.xlsx");
        excelImport.getFirstSheetData();
    }


}
